package com.zzm.shortlink.admin.controller;

import com.zzm.shortlink.admin.common.biz.user.UserContext;
import com.zzm.shortlink.admin.common.convention.result.Result;
import com.zzm.shortlink.admin.common.convention.result.Results;
import com.zzm.shortlink.admin.dto.req.ShortLinkGroupRemoveReqDTO;
import com.zzm.shortlink.admin.dto.req.ShortLinkGroupSaveReqDTO;
import com.zzm.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import com.zzm.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import com.zzm.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import com.zzm.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    /**
     * 添加分组
     * @param requestParam 分组信息
     * @return 成功结果
     */
    @PostMapping("/api/short-link/admin/v1/group")
    public Result<Void> save(@RequestBody ShortLinkGroupSaveReqDTO requestParam) {
        String username = "";
        if(requestParam.getUsername()==null) {
            username =  UserContext.getUsername();
        }
        groupService.saveGroup(requestParam.getName(),username);
        return Results.success();
    }

    /**
     * 获取短链接分组
     * @return 分组列表
     */

    @GetMapping("/api/short-link/admin/v1/group")
    public Result<List<ShortLinkGroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }
    /**
     * 修改短链接分组
     * @return 成功信息
     */
    @PutMapping("/api/short-link/admin/v1/group")
    public Result<Void> update(@RequestBody ShortLinkGroupUpdateReqDTO requestParam) {
        groupService.updateGroup(requestParam);
        return Results.success();
    }

    /**
     * 删除短链接分组
     * @return 成功信息
     */
    @DeleteMapping("/api/short-link/admin/v1/group")
    public Result<Void> remove( @RequestParam("gid") String gid) {
        groupService.removeGroup(gid);
        return Results.success();
    }
    /**
     * 短链接分组排序
     */
    @PostMapping("/api/short-link/admin/v1/group/sort")
    public Result<Void> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> requestParam) {
        groupService.sortGroup(requestParam);
        return Results.success();
    }

}

